package org.inote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.NotFoundException;
import org.inote.dao.NoteMapper;
import org.inote.dao.UserMapper;
import org.inote.pojo.tablePojo.Note;
import org.inote.pojo.voPojo.NoteVo;
import org.inote.service.NoteService;
import org.inote.utils.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import sun.print.PeekGraphics;

import javax.annotation.Resource;
import java.util.List;
@Service
public class NoteServiceImpl implements NoteService {

    @Resource
    private NoteMapper noteMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public boolean addNote(Note note,Integer userId) {
        noteMapper.addNote(note);
        userMapper.addUserNoteCount(userId);
        return true;
    }

    @Override
    public boolean deleteNote(Integer noteId,Integer userId) {

        noteMapper.deleteNote(noteId);
        userMapper.subUserNoteCount(userId);
        return true;
    }

    @Override
    public List<Note> queryAllNotes() {
        return noteMapper.queryAllNotes();
    }

    @Override
    public List<Note> queryUserAllNotes(Integer userId) {
        return noteMapper.queryUserAllNote(userId);
    }

    @Override
    public NoteVo getAndConvert(Integer noteId) {
        NoteVo noteVo = noteMapper.getNoteVoById(noteId);
        if(noteVo == null)
        {
            return null;
        }
        NoteVo noteVo1 = new NoteVo();
        BeanUtils.copyProperties(noteVo,noteVo1);
        String noteText = noteVo1.getNoteText();
        noteVo1.setNoteText(MarkdownUtils.markdownToHtmlExtensions(noteText));
        return noteVo1;
    }

    @Override
    public PageInfo<NoteVo> queryByPage(Integer pageNum,String condition) {
        PageHelper.startPage(pageNum,5);
        List<NoteVo> noteVos = noteMapper.queryByPage(condition);
        System.out.println(noteVos);
        PageInfo<NoteVo> info = new PageInfo<>(noteVos);
        return info;
    }

    @Override
    public List<Note> queryNoteByContent() {
        return noteMapper.queryNoteByContent();
    }

    @Override
    public int updateCommentCount(Integer noteId) {
        return noteMapper.updateCommentCount(noteId);
    }
}
